home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ham Radio 2000
/
Ham Radio 2000.iso
/
ham2000
/
tcp_ip
/
gracil
/
p10dvr
/
rmtcfg.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-07
|
6KB
|
285 lines
/****************************************************************************
*
* COPYRIGHT 1990,91,92 BY GRACILIS INC.
*
* 623 Palace St.
* Aurora, Il. 60506
*
* (708)-801-8800 Office
* (708)-844-0183 (FAX - Support BBS)
*
* GRACILIS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS
* SOFTWARE FOR ANY PURPOSE.
*
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
*
* Permission is granted for non-commercial use/distribution only, as long as
* this copyright header is included intact and unaltered.
*
******************************************************************************/
/* Remote Configuration Server */
#include <stdio.h>
#include <string.h>
#ifdef PACKETEN_PC
#include <time.h>
#include <sys/timeb.h>
#endif
#include "global.h"
#include "files.h"
#include "mbuf.h"
#include "socket.h"
#include "session.h"
#include "proc.h"
#include "dirutil.h"
#include "commands.h"
#include "mailbox.h"
#include "config.h"
#include "cmdparse.h"
#include "netuser.h"
#include "rmtcfg.h" /* get the port # for the server to watch */
extern struct cmds Cmds[];
#ifdef PACKETEN /* Are we building for a P10 itself? */
#define RMTCFG_CLIENT 1
#endif
#ifdef PACKETEN_PC /* OR or we build a PC load to talk to a P10? */
#define RMTCFG_SERVER 1
#define RMTCFG_CLIENT 1
#endif
static int Sremotesrv = -1; /* Prototype socket for service */
#ifdef RMTCFG_SERVER
char *Rmtcfg_dir="/rmtcfg"; /* Base directory for config files... */
static void rmtcfg_srv __ARGS((int s,void *unused,void *p));
/* Start up remote configuration service */
int
rmtcfg_start(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct sockaddr_in lsocket;
int s;
if(Sremotesrv != -1){
return 0;
}
psignal(Curproc,0); /* Don't keep the parser waiting */
chname(Curproc,"RmtCfg listener");
lsocket.sin_family = AF_INET;
lsocket.sin_addr.s_addr = INADDR_ANY;
if(argc < 2)
lsocket.sin_port = IPPORT_RMTCFG;
else
lsocket.sin_port = atoi(argv[1]);
Sremotesrv = socket(AF_INET,SOCK_STREAM,0);
bind(Sremotesrv,(char *)&lsocket,sizeof(lsocket));
listen(Sremotesrv,1);
for(;;){
if((s = accept(Sremotesrv,NULLCHAR,(int *)NULL)) == -1)
break; /* Service is shutting down */
/* Spawn a server */
newproc("RmtCfg server",1024,rmtcfg_srv,s,NULL,NULL,0);
}
return 0;
}
static void
rmtcfg_srv(s,unused,p)
int s;
void *unused;
void *p;
{
char configfile[80];
FILE *fp;
char *file;
sockmode(s,SOCK_ASCII);
sockowner(s,Curproc);
log(s,"Remote Config Start");
/* Wait for the name of the requested file from the remote end */
recvline(s,configfile,80);
rip(configfile);
/* If no config file is specified... */
if(strlen(configfile) == 0)
{
usprintf(s,"#Rmtcfg Server: No Filename Specified!\n");
}
else
{
rmtcfg_read(s,configfile);
}
close_s(s);
log(s,"Remote Config Complete");
}
int
rmtcfg_stop(argc,argv,p)
int argc;
char *argv[];
void *p;
{
close_s(Sremotesrv);
Sremotesrv = -1;
return 0;
}
/* Open a requested config file, read and send it to the given socket */
rmtcfg_read(s,filename)
int s;
char *filename;
{
FILE *fp;
char *file; /* derived file name */
int sts;
char buf[81];
file = (char *)pathname(Rmtcfg_dir,filename);
log(s,"Opening config file: %s\n",file);
if((fp = fopen(file,READ_TEXT)) == NULLFILE)
{
usprintf(s,"#Config file: %s is unavailable.\n",file);
free(file);
return(0);
}
free(file); /* free up the filename string */
/* Send him a comment line with the filename in it... */
usprintf(s,"#Config file: %s:\n",file);
while(fgets(buf,sizeof(buf),fp),!feof(fp))
{
usprintf(s,"%s",buf);
pwait(NULL); /* give up the processor for others to run */
}
fclose(fp);
return (0);
}
#endif /* RMTCFG_SERVER */
#ifdef RMTCFG_CLIENT
/* Remote configuration client */
int do_rmtcfg_request __ARGS((int argc,char *argv[],void *p));
/* Request configuration data from a remote server */
do_rmtcfg_request(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct sockaddr_in sock;
int s; /* socket # to use */
char linebuf[81]; /* temp storage */
char linetmp[81]; /* temp storage */
int lineno;
char ipaddr_buf[20];
if(argc < 3)
{
tprintf("Usage: rmtcfg <server> <filename> [optional TCP Port #]\n");
return 1;
}
sock.sin_family = AF_INET;
if(argc >= 4)
{
sock.sin_port = atoi(argv[3]);
if(sock.sin_port == 0)
{
tprintf("Invalid PORT number: %s\n",argv[3]);
return 1;
}
}
else sock.sin_port = IPPORT_RMTCFG;
/* resolve the Rmtcfg_servers IP address... */
if((sock.sin_addr.s_addr = resolve(argv[1])) == 0){
/* If unable to resolve the address...*/
tprintf("Rmtcfg: %s is unknown.\n",argv[1]);
return 1;
}
tprintf("Rmtcfg: File %s Host %s TCP port: %d.\n",
argv[2],argv[1],sock.sin_port);
/* Allocate the socket... */
if((s = socket(AF_INET,SOCK_STREAM,0)) == -1){
/* If cant get a socket... */
tprintf("Rmtcfg: Can't allocate a Socket!\n");
return 1;
}
sockmode(s,SOCK_ASCII);
/* Only wait for 1 minute... if no answer... then assume */
/* the server is down... */
alarm((long)(60*(1000L/MSPTICK)));
/* establish the connection... */
if(connect(s,(char *)&sock,sizeof(sock)) == -1){
/* couldn't connect... */
close_s(s);
alarm(0L);
tprintf("Rmtcfg: Unsuccessful connect attempt.\n");
return 1;
}
alarm(0L);
/* tell the Rmtcfg server what file to send us... */
usprintf(s,"%s\n",argv[2]);
usflush(s);
/* The bootup process better not take more than 60 Secs... */
alarm((long)(60*(1000L/MSPTICK)));
lineno = 0;
/* receive data till there is no more... */
while(recvline(s,linebuf,80) >= 0)
{
tprintf("%s",linebuf);
strcpy(linetmp,linebuf);
if(cmdparse(Cmds,linebuf,NULL) != 0)
{
tprintf("input line: %s",linetmp);
}
lineno++;
}
alarm(0L);
/* close the connection... */
close_s(s);
/* Return successful completion of bootserver conversation */
return 0;
}
#endif